﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Common.Providers;
using Vit.Web;
using Vit.Web.Modules;
using Vit.Web.Models;
using Vit.Web.Providers;
using Vit.Web.Providers.DataSource;
using Vit.Web.Extensions;
using Vit.Web.Helpers;
using Vit.Web.Providers.Captcha;

namespace Vit.Web.SqlProviders
{
    public class SqlMainProviderModule : ModuleBase
    {
        public SqlMainProviderModule()
        {
            Version = new Version(1, 1, 1);
        }

        public override void OnApplicationStart(HttpApplication application, IUnityContainer unity)
        {
            PortalHelper.RegisterWidgets(typeof(Vit.Web.Mvc.Controllers.ControllerBase).Assembly);

            ICacheProvider dataCache = unity.Resolve<ICacheProvider>();
            unity
                .RegisterInstance(new WebProvidersDb(), new HttpContextRequestLifetimeManager())
                // register providers
                .RegisterType<IRandomTextGenerator, RNGRandomTextGenerator>(new ContainerControlledLifetimeManager())
                .RegisterType<ICaptchaGenerator, DefaultCaptchaGenerator>(new ContainerControlledLifetimeManager())
                .RegisterType<IFriendlyUrlProvider, SqlFriendlyUrlProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<ILogProvider, SqlLogProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<IMembershipProvider, SqlMembershipProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<INavigationProvider, SqlNavigationProvider>(new HttpContextRequestLifetimeManager())
                .RegisterType<IPortalProvider, SqlPortalProvider>(new HttpContextRequestLifetimeManager())
                // register enum datasources
                .RegisterInstance<IDataSourceProvider>("DataSource:DataStatus", new EnumDataSourceProvider(typeof(DataStatus), dataCache), new ContainerControlledLifetimeManager())
                .RegisterInstance<IDataSourceProvider>("DataSource:LogLevel", new EnumDataSourceProvider(typeof(LogLevel), dataCache), new ContainerControlledLifetimeManager())
                .RegisterInstance<IDataSourceProvider>("DataSource:Sex", new EnumDataSourceProvider(typeof(Sex), dataCache), new ContainerControlledLifetimeManager())
                // register database datasources
                .RegisterInstance<IDataSourceProvider>("DataSource:DataSourceCategory", new DbContextEntitySourceProvider(typeof(WebProvidersDb), typeof(DataSourceCategory), "CategoryName", "CategoryName", dataCache), new ContainerControlledLifetimeManager())
                .RegisterInstance<IDataSourceProvider>("DataSource:NavigationGroup", new DbContextEntitySourceProvider(typeof(WebProvidersDb), typeof(NavigationGroup), "Caption", "Name", dataCache), new ContainerControlledLifetimeManager())
                .RegisterInstance<IDataSourceProvider>("DataSource:NavigationItem", new DbContextEntitySourceProvider(typeof(WebProvidersDb), typeof(NavigationItem), "Caption", "Name", dataCache), new ContainerControlledLifetimeManager())
                // register data providers
                .RegisterType<IDataProvider<DataSourceCategory>, SqlDataSourceCategoryDataProvider>()
                .RegisterType<IDataProvider<DataSourceItem>, SqlDataSourceDataProvider>()
                .RegisterType<IDataProvider<FriendlyUrl>, SqlFriendlyUrlDataProvider>()
                .RegisterType<IDataProvider<LocalizationItem>, SqlLocalizationItemProvider>()
                .RegisterType<IDataProvider<Log>, SqlLogDataProvider>()
                .RegisterType<IDataProvider<NavigationGroup>, SqlNavigationGroupDataProvider>()
                .RegisterType<IDataProvider<NavigationItem>, SqlNavigationItemDataProvider>()
                .RegisterType<IDataProvider<User>, SqlUserDataProvider>()
                .RegisterType<IDataProvider<Role>, SqlRoleDataProvider>()
                .RegisterType<IDataProvider<WebPage>, SqlWebPageDataProvider>();
        }

        public override bool OnInstalling(object sender, EventArgs e, out string message)
        {
            message = string.Empty;
            try
            {
                WebProvidersDb db = new WebProvidersDb();
                return db.Database.CreateIfNotExists();
            }
            catch (Exception ex)
            {
                message = ex.GetBaseException().Message;
                return false;
            }
        }

        public override string Name
        {
            get
            {
                return "SqlMainProviderModule";
            }
        }
    }
}
